package com.liy.cloud.common.trade.entity;

import com.liy.cloud.common.trade.utils.TradeMessageParser;

import java.io.Serializable;

/**
 * 入站交易类
 * 
 * @author shitou
 * @Time：2018年8月17日 上午9:44:42
 * @version 1.0 Function:
 */
public class TradeEnterStationVO extends TradeComm implements Serializable {

	private static final long serialVersionUID = 1L;

	/*
	 * 审核员
	 */
	private String operId;

	/**
	 * 默认构造方法
	 **/
	public TradeEnterStationVO() {
		super();
	}

	public String getOperId() {
		return operId;
	}

	public void setOperId(String operId) {
		this.operId = operId;
	}

	/**
	 * 根据版本容解析交易内容
	 * 
	 * @param message
	 * @throws Exception
	 */
	public TradeEnterStationVO init(String message) throws Exception {

		TradeMessageParser messageParser = null;
		if(message.startsWith("SZMETROV2")) {
			// 根据报文格式长度来定义， -1为行业分割符
			int[] splitNumber = { 9, 8, 9, 2, 10, 9, 6, 6, 14, 10, 5, 10, 20, 10, 2, 2, 16, 1, 1, 2, 2, 2, 4, 4, 1, 4, 5,
					-1, 5 };
			messageParser = new TradeMessageParser(message, splitNumber);
			
			this.setVersion(messageParser.getFiled(0));
			this.setTac(messageParser.getFiled(1));
			this.setCardId(messageParser.getFiled(2));
			this.setTradeCode(messageParser.getFiled(3));
			this.setTradeAddress(messageParser.getFiled(4));

			this.setDeviceId(messageParser.getFiled(5));
			this.setOperator(messageParser.getFiled(6));
			this.setOperId(messageParser.getFiled(7));
			this.setTradeTime(messageParser.getFiled(8));
			this.setDeviceSeq(Long.parseLong(messageParser.getFiled(9)));

			this.setCardSeq(Long.parseLong(messageParser.getFiled(10)));
			this.setDeviceSum(Long.parseLong(messageParser.getFiled(11)));
			this.setSamcardId(messageParser.getFiled(12));
			this.setSamcardSeq(Long.parseLong(messageParser.getFiled(13)));
			this.setCardCode(messageParser.getFiled(14));
			this.setTradeSub(messageParser.getFiled(15));
			this.setPhysicalId(messageParser.getFiled(16));

			this.setTestFlag(messageParser.getFiled(17));
			this.setGreyFlag(messageParser.getFiled(18));
			this.setMedia(messageParser.getFiled(19));
			this.setCardVersion(messageParser.getFiled(20));
			this.setSubCardCode(messageParser.getFiled(21));

			this.setCityCode(messageParser.getFiled(22));
			this.setBusinessCode(messageParser.getFiled(23));

			this.setIndustrySplit(messageParser.getFiled(24));
			this.setIssuer(messageParser.getFiled(25));
			super.setIndustryDataLength(Long.parseLong(messageParser.getFiled(26)));
			super.setIndustryData(messageParser.getFiled(27));
			super.setCrc16(messageParser.getFiled(28));
			
		} else if(message.startsWith("SZMETROV3")) {
			
			/** 根据报文格式长度来定义， -1为行业分割符
			 * 一、V3卡号由9位变为20位
			 * 二、增加字段：
				           付费方式
					支付方式
					发卡方机构码
					发卡方交易流水号
					运营模式
			      三、票卡发行方标识	从长度4变为长度2
			 */
			// -----------9
			int[] splitNumber = { 
					9, 8, 20, 2, 10, 9, 6, 6, 14, 10, 5, 10, 20, 10, 2, 2, 16, 1, 1, 2, 2, 2, 4, 4, 
					1, 2, 8, 10, 4,	//V3增加的字段
					1, 2, 5,
					-1, 5 };
			messageParser = new TradeMessageParser(message, splitNumber);
			this.setVersion(messageParser.getFiled(0));
			this.setTac(messageParser.getFiled(1));
			this.setCardId(messageParser.getFiled(2));
			this.setTradeCode(messageParser.getFiled(3));
			this.setTradeAddress(messageParser.getFiled(4));

			this.setDeviceId(messageParser.getFiled(5));
			this.setOperator(messageParser.getFiled(6));
			this.setOperId(messageParser.getFiled(7));
			this.setTradeTime(messageParser.getFiled(8));
			this.setDeviceSeq(Long.parseLong(messageParser.getFiled(9)));

			this.setCardSeq(Long.parseLong(messageParser.getFiled(10)));
			this.setDeviceSum(Long.parseLong(messageParser.getFiled(11)));
			this.setSamcardId(messageParser.getFiled(12));
			this.setSamcardSeq(Long.parseLong(messageParser.getFiled(13)));
			this.setCardCode(messageParser.getFiled(14));
			this.setTradeSub(messageParser.getFiled(15));
			this.setPhysicalId(messageParser.getFiled(16));

			this.setTestFlag(messageParser.getFiled(17));
			this.setGreyFlag(messageParser.getFiled(18));
			this.setMedia(messageParser.getFiled(19));
			this.setCardVersion(messageParser.getFiled(20));
			this.setSubCardCode(messageParser.getFiled(21));

			this.setCityCode(messageParser.getFiled(22));
			this.setBusinessCode(messageParser.getFiled(23));

			// V3增加字段
			this.setPayMethod(messageParser.getFiled(24));
			this.setPayType(messageParser.getFiled(25));
			this.setIssuerCode(messageParser.getFiled(26));
			this.setIssuerSeq(messageParser.getFiled(27));
			this.setOperMode(messageParser.getFiled(28));
			
			this.setIndustrySplit(messageParser.getFiled(29));
			this.setIssuer(messageParser.getFiled(30));
			super.setIndustryDataLength(Long.parseLong(messageParser.getFiled(31)));
			super.setIndustryData(messageParser.getFiled(32));
			super.setCrc16(messageParser.getFiled(33));
		}


		return this;
	}
}